    <?php
        ini_set('date.timezone','Asia/Shanghai');
        //error_reporting(E_ERROR);
        require_once "../lib/WxPay.Api.php";
        require_once "WxPay.JsApiPay.php";
        require_once 'log.php';

        //初始化日志
        $logHandler= new CLogFileHandler("../logs/".date('Y-m-d').'.log');
        $log = Log::Init($logHandler, 15);
        //①、获取用户openid
        $tools     = new JsApiPay();
        $code      = $_GET['code'];
        $title     = $_GET['title'];
        if(!$title){
            $title = "寻爱";
        }
        $total_fee = $_GET['total_fee']*100;
        //$total_fee = 1;
        ////appid: //wx5a353e27bf3fc24f  47418e8e10857b23cd110e4a92717506
        $apiUrl    = "https://api.weixin.qq.com/sns/jscode2session?appid=wx5a353e27bf3fc24f&secret=47418e8e10857b23cd110e4a92717506&js_code={$code}&grant_type=authorization_code";
        $apiData   = json_decode(file_get_contents($apiUrl,true), true);
        $openIds   = $apiData['openid'];
    //echo json_encode(array("openid"=>$openIds));
        if($openIds){
            $openId = $openIds;
        }

        //②、统一下单
        $input = new WxPayUnifiedOrder();
        $input->SetBody($title);
        $input->SetAttach("test");

        $trade_no = WxPayConfig::MCHID.date("YmdHis");
        $input->SetOut_trade_no($trade_no);//商户订单号  商户号+时间戳

        $input->SetTotal_fee($total_fee);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 600));
        $input->SetGoods_tag("");//图片
        $input->SetNotify_url("https://www.bozhiyingxiao.com/little_program/store_love/pay/example/notify.php");
        $input->SetTrade_type("JSAPI");
        $input->SetOpenid($openId);
        $order = WxPayApi::unifiedOrder($input);
        $jsApiParameters = $tools->GetJsApiParameters($order);
        $jsApiParameters = json_decode($jsApiParameters);
        echo json_encode(array("pay"=>$jsApiParameters,"no"=>$trade_no,"op"=>$openIds));
?>
